package br.com.portalecbahia.customize;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.portalecbahia.R;
import br.com.portalecbahia.model.Match;

import com.raptureinvenice.webimageview.image.WebImageView;

public class MatchListAdapter extends BaseAdapter {
    
	private Context context;

    private List<Match> listMatch;
    public static int positionFirst = 0;
   
    public MatchListAdapter (Context context, List<Match> listMatch){
        this.context = context;
        this.listMatch = listMatch;
        positionFirst = 0;
    }
    
	public int getCount() {
		// TODO Auto-generated method stub
		return  listMatch.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listMatch.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
        // Get actual object
		Match match = listMatch.get(position);
		 // on View
        LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.item_list_match , null);
        
        if(match.getDate()!=null){
            TextView dayTX = (TextView)view.findViewById(R.id.dayTX);
            if(match.getDayDate().toString().length()<2)
            	dayTX.setText("0"+match.getDayDate().toString());
            else
            	dayTX.setText(match.getDayDate().toString());
            
            TextView monthTX = (TextView)view.findViewById(R.id.monthTX);
            if(match.getMonthDate().toString().length()<2)
            	monthTX.setText("0"+match.getMonthDate().toString());
            else
            	monthTX.setText(match.getMonthDate().toString());
            
            TextView yearTX = (TextView)view.findViewById(R.id.yearTX);
            yearTX.setText(match.getYearDate().toString());

        }
        
        if(match.getId()==null){
        	ImageView lineLL =(ImageView)view.findViewById(R.id.lineIMG);
        	lineLL.setVisibility(View.INVISIBLE);
        	
            TextView separatorTX = (TextView)view.findViewById(R.id.separatorTX);
            separatorTX.setVisibility(View.INVISIBLE);

            
            ImageView arrowIMG = (ImageView) view.findViewById(R.id.arrowIMG);
            arrowIMG.setVisibility(View.INVISIBLE);

        	
        }
        
        if(match.getHomeTeamName()!=null){
            TextView homeTeamNameTX = (TextView)view.findViewById(R.id.homeTeamNameTX);
            homeTeamNameTX.setText(match.getHomeTeamName());

        }
        if(match.getAwayTeamName()!=null){
            TextView awayTeamNameTX = (TextView)view.findViewById(R.id.awayTeamNameTX);
            awayTeamNameTX.setText(match.getAwayTeamName());

        }
        
        WebImageView flagHomeTeamTMG = (WebImageView) view.findViewById(R.id.flagHomeIMG);
        if(match.getHomeTeamUrlImage() !=null){
        	WebImageView.setDiskCachingDefaultCacheTimeout(60 * 60* 24*30); // 1 month expiration
        	flagHomeTeamTMG.setImageWithURL(context,  match.getHomeTeamUrlImage());        	 
        }
        
        WebImageView flagAwayTeamTMG = (WebImageView) view.findViewById(R.id.flagAwayIMG);
        if(match.getAwayTeamUrlImage() !=null){
        	WebImageView.setDiskCachingDefaultCacheTimeout(60 * 60* 24*30); // 1 month expiration
        	flagAwayTeamTMG.setImageWithURL(context,  match.getAwayTeamUrlImage());        	 
        }
        
        SimpleDateFormat formatUTC = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date now=null;
		try {
			now = (Date) (new SimpleDateFormat("yyyy-MM-dd HH:mm")).parse(formatUTC.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
        ImageView scoreHomeIMG = (ImageView) view.findViewById(R.id.scoreHomeIMG);
        ImageView scoreAwayIMG = (ImageView) view.findViewById(R.id.scoreAwayIMG);
        
       
        if(match.getHomeGoal()!=null && !now.before(match.getDateUTC()) ){
        	if(match.getHomeGoal()==0){
        		scoreHomeIMG.setImageResource(R.drawable.zero);
        	}
        	else if(match.getHomeGoal()==1){
        		scoreHomeIMG.setImageResource(R.drawable.one);
        	}
        	else if(match.getHomeGoal()==2){
        		scoreHomeIMG.setImageResource(R.drawable.two);
		    }
        	else if(match.getHomeGoal()==3){
        		scoreHomeIMG.setImageResource(R.drawable.three);
        	}
        	else if(match.getHomeGoal()==4){
        		scoreHomeIMG.setImageResource(R.drawable.four);
        	}
        	else if(match.getHomeGoal()==5){
        		scoreHomeIMG.setImageResource(R.drawable.five);
        	}
        	else if(match.getHomeGoal()==6){
        		scoreHomeIMG.setImageResource(R.drawable.six);
        	}
        	else if(match.getHomeGoal()==7){
        		scoreHomeIMG.setImageResource(R.drawable.seven);
        	}
        	else if(match.getHomeGoal()==8){
        		scoreHomeIMG.setImageResource(R.drawable.eight);
        	}
        	else if(match.getHomeGoal()==9){
        		scoreHomeIMG.setImageResource(R.drawable.nine);
        	}else{
        		scoreHomeIMG.setVisibility(View.INVISIBLE);	
        	}
        }else{
        	
        	scoreHomeIMG.setVisibility(View.INVISIBLE);
        }
	        
	        
        if(match.getAwayGoal()!=null && !now.before(match.getDateUTC()) ){
        	if(match.getAwayGoal()==0){
        		scoreAwayIMG.setImageResource(R.drawable.zero);
        	}
        	else if(match.getAwayGoal()==1){
        		scoreAwayIMG.setImageResource(R.drawable.one);
        	}
        	else if(match.getAwayGoal()==2){
        		scoreAwayIMG.setImageResource(R.drawable.two);
        	}
        	else if(match.getAwayGoal()==3){
        		scoreAwayIMG.setImageResource(R.drawable.three);
        	}
        	else if(match.getAwayGoal()==4){
        		scoreAwayIMG.setImageResource(R.drawable.four);
        	}
        	else if(match.getAwayGoal()==5){
        		scoreAwayIMG.setImageResource(R.drawable.five);
        	}
        	else if(match.getAwayGoal()==6){
        		scoreAwayIMG.setImageResource(R.drawable.six);
        	}
        	else if(match.getAwayGoal()==7){
        		scoreAwayIMG.setImageResource(R.drawable.seven);
        	}
        	else if(match.getAwayGoal()==8){
        		scoreAwayIMG.setImageResource(R.drawable.eight);
        	}
        	else if(match.getAwayGoal()==9){
        		scoreAwayIMG.setImageResource(R.drawable.nine);
        	}else{
        		scoreAwayIMG.setVisibility(View.INVISIBLE);	
        	}
        }else{
        	scoreAwayIMG.setVisibility(View.INVISIBLE);
	    }
        
       
        TextView informationPlaceHourTX = (TextView)view.findViewById(R.id.informationPlaceHourTX);
        if(match.getChampionshipName() !=null && match.getStadimName() !=null && match.getDate() !=null){
        	informationPlaceHourTX.setText(match.getChampionshipName()+" | "+match.getHourDate()+" - " +match.getStadimName());
        }
        TextView aovivoTX = (TextView)view.findViewById(R.id.aovivoTX);
        if(match.getId()!=null && !match.getFinished()){
        aovivoTX.setVisibility(View.VISIBLE);
        }else{
            aovivoTX.setVisibility(View.INVISIBLE);
            flagHomeTeamTMG.setBackgroundColor(Color.WHITE);
            flagAwayTeamTMG.setBackgroundColor(Color.WHITE);

        }
        
        if(match.getId()!=null && match.getLive()){
            aovivoTX.setVisibility(View.VISIBLE);

        }else{
            aovivoTX.setVisibility(View.INVISIBLE);

        }
        return view;
	}

	
	 
	
	

}
